import { Injectable } from '@angular/core';

// 导入xml json转换器
import {Parser} from 'xml2js';
import {Observable, of} from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class OnvifWsService {

  constructor() {}

  const
  host = 'http://127.0.0.1:8080';

  parseXml(xmlStr: string): string {
    let result;
    const parser = new Parser();
    parser.parseString(xmlStr, (e, r) => { result = r; });
    return result;
  }

  /**
   * 获取设备DNS信息
   */
  getDNS(dns): void {
    const url = `${this.host}/dns`;

    const that = this;
    console.log(url);
    $.get(url, function (result) {

      console.log(that.parseXml(result));
      dns(that.parseXml(result));
    });
  }

  /**
   * 获取设备基本信息
   */
  getDeviceInformation(information): void {
    const url = `${this.host}/device/information`;

    const that = this;
    console.log(url);
    $.get(url, function (result) {

      console.log(that.parseXml(result));
      information(that.parseXml(result));
    });
  }

  /**
   * 获取设备网关信息
   */
  getNetworkDefaultGateway(gateway): void {
    const url = `${this.host}/device/network/gateway`;

    const that = this;
    console.log(url);
    $.get(url, function (result) {

      console.log(that.parseXml(result));
      gateway(that.parseXml(result));
    });
  }

  /**
   * 获取设备网络信息
   */
  getNetworkInterfaces(network): void {
    const url = `${this.host}/device/network/interfaces`;

    const that = this;
    console.log(url);
    $.get(url, function (result) {

      console.log(that.parseXml(result));
      network(that.parseXml(result));
    });
  }

  /**
   * 获取设备发现模式
   */
  getDiscoveryMode(model) {
    const url = `${this.host}/device/discovery/mode`;

    const that = this;
    console.log(url);
    $.get(url, function (result) {

      console.log(that.parseXml(result));
      model(that.parseXml(result));
    });
  }

  /**
   * 云台控制接口
   */
  ptzControl(cmd: string) {
    const url = `${this.host}/ptz/control/${cmd}`;

    const that = this;
    console.log(url);
    $.get(url, function (result) {

      console.log(that.parseXml(result));
    });
  }
}
